#include	"ParseXML.h"

using namespace	std;
ParseXML::ParseXML()
{
}
ParseXML::~ParseXML()
{
}
// Method
void			ParseXML::ParsePokemon(std::vector<Pokemon *> &list)
{
	QString fileName = "pokedata.xml";
	QFile file(fileName);
	file.open(QFile::ReadOnly | QFile::Text);
	list.resize(500);
	QDomDocument doc;
	doc.setContent(&file, false);
	 
	QDomElement racine = doc.documentElement();

	racine = racine.firstChildElement();

	while(!racine.isNull())
	{
		if (racine.tagName() == "pokemon")
 		{
			Pokemon			*currPokemon = new Pokemon;
			QDomElement elem = racine;
			QString attFaq = elem.attribute("id");
		
			list[atoi(elem.attribute("id").toStdString().c_str()) - 1] = currPokemon;
			currPokemon->setNumber(atoi(elem.attribute("id").toStdString().c_str()));
			elem = elem.firstChildElement();
			while (!elem.isNull())
			{
				if (elem.tagName() == "stats")
					getStats(elem, currPokemon);
				else if (elem.tagName() == "evolutions")
					getEvolutions(elem, currPokemon);
				else if (elem.tagName() == "ratio")
					getRatio(elem, currPokemon);
				else if (elem.tagName() == "moves")
					getMoves(elem, currPokemon);
				else if (elem.tagName() == "type")
					currPokemon->addTypes(elem.text().toStdString());
				else if (elem.tagName() == "egg-group")
					currPokemon->addEggGroup(elem.text().toStdString());
				else if (elem.tagName() == "ability")
					currPokemon->setAbility(elem.text().toStdString());
				else if (elem.tagName() == "exp")
					currPokemon->setExp(atoi(elem.text().toStdString().c_str()));
				else if (elem.tagName() == "name")
					currPokemon->setName(elem.text().toStdString());
				else if (elem.tagName() == "species")
					currPokemon->setSpecies(elem.text().toStdString());
				else if (elem.tagName() == "height")
					currPokemon->setHeight(atof(elem.text().toStdString().c_str()));
				else if (elem.tagName() == "weight")
					currPokemon->setWeight(atof(elem.text().toStdString().c_str()));
				else if (elem.tagName() == "description")
					currPokemon->setDescription(elem.text().toStdString());
				elem = elem.nextSiblingElement();
			}
		}
		racine = racine.nextSiblingElement();
	}
	return ;
}

void			ParseXML::ParseSkills(map<string, Skill *> &list)
{
	QString fileName = "Mdata.xml";
	QFile file(fileName);
	file.open(QFile::ReadOnly | QFile::Text);
	
	QDomDocument doc;
	doc.setContent(&file, false);
	 
	QDomElement racine = doc.documentElement();
	Skill		*currSkill;
	racine = racine.firstChildElement();
	while (!racine.isNull())
	{
		currSkill = new Skill;
		QDomElement	elem;
		elem = racine.firstChildElement();
		while (!elem.isNull())
		{
			if (elem.tagName() == "TM" || elem.tagName() == "HM")
			{
				currSkill->setSkillLearnType(elem.tagName().toStdString());
				currSkill->setId(atoi(elem.attribute("id").toStdString().c_str()));
			}
			else if (elem.tagName() == "name")
				currSkill->setSkillName(elem.text().toStdString());
			else if (elem.tagName() == "kind")
				currSkill->setKind(elem.text().toStdString());
			else if (elem.tagName() == "type")
				currSkill->setType(elem.text().toStdString());
			else if (elem.tagName() == "PP")
			currSkill->setPP(atoi(elem.text().toStdString().c_str()));
			else if (elem.tagName() == "power")
				currSkill->setPower(atoi(elem.text().toStdString().c_str()));
			else if (elem.tagName() == "accuracy")
				currSkill->setPower(atoi(elem.text().toStdString().c_str()));
			else if (elem.tagName() == "information")
				currSkill->setInfo(elem.text().toStdString());
			else if (elem.tagName() == "location")
				currSkill->setLocation(elem.text().toStdString());
			else if (elem.tagName() == "suggestion")
				currSkill->setSuggestion(elem.text().toStdString());
			elem = elem.nextSiblingElement();
		}
		list.insert(make_pair(currSkill->getSkillName(), currSkill));
		racine = racine.nextSiblingElement();
	}
	return ;
}

void			ParseXML::getStats(QDomElement &elem, Pokemon *currPokemon)
{
	QDomElement	sousElem;

	sousElem = elem.firstChildElement();
	while (!sousElem.isNull())
	{
		if (sousElem.tagName() == "HP")
			currPokemon->getStats().setHP(atoi(sousElem.text().toStdString().c_str()));
		else if (sousElem.tagName() == "ATK")
			currPokemon->getStats().setAtk(atoi(sousElem.text().toStdString().c_str()));
		else if (sousElem.tagName() == "DEF")
			currPokemon->getStats().setDef(atoi(sousElem.text().toStdString().c_str()));
		else if (sousElem.tagName() == "SPD")
			currPokemon->getStats().setSpd(atoi(sousElem.text().toStdString().c_str()));
		else if (sousElem.tagName() == "SAT")
			currPokemon->getStats().setSat(atoi(sousElem.text().toStdString().c_str()));
		else if (sousElem.tagName() == "SDF")
			currPokemon->getStats().setSdf(atoi(sousElem.text().toStdString().c_str()));
		sousElem = sousElem.nextSiblingElement();
	}
	return ;
}

void			ParseXML::getRatio(QDomElement &elem, Pokemon *currPokemon)
{
	QDomElement	sousElem;
	float		male = 0.0, female = 0.0;
	sousElem = elem.firstChildElement();
	while (!sousElem.isNull())
	{
		if (sousElem.tagName() == "male")
		{
			male = atof(sousElem.text().toStdString().c_str());
		}
		else if (sousElem.tagName() == "female")
		{
			female = atof(sousElem.text().toStdString().c_str());
		}
		sousElem = sousElem.nextSiblingElement();
	}
	currPokemon->setRatio(make_pair(male, female));
	return ;
}

void			ParseXML::getEvolutions(QDomElement &elem, Pokemon *currPokemon)
{
	vector<pair<string, int> >		tabEvolutions;
	QDomElement	sousElem;
	sousElem = elem.firstChildElement();
	int			lvl = 1;
	string		evolName = "";
	while (!sousElem.isNull())
	{
		lvl = 1;
		evolName = "";
		QDomElement	sousElem2;
		sousElem2 = sousElem.firstChildElement();
		while (!sousElem2.isNull())
		{
			if (sousElem2.tagName() == "name")
				evolName = sousElem2.text().toStdString();
			else if (sousElem2.tagName() == "lvl")
				lvl = atoi(sousElem2.text().toStdString().c_str());
			sousElem2 = sousElem2.nextSiblingElement();
		}
		tabEvolutions.push_back(make_pair(evolName, lvl));
		sousElem = sousElem.nextSiblingElement();
	}
	currPokemon->setEvolutions(tabEvolutions);
}

void			ParseXML::getMoves(QDomElement &elem,Pokemon *currPokemon)
{
	QDomElement	sousElem;
	string		name;
	int			lvl;
	std::vector<std::pair<std::string, int> > tab;

	sousElem = elem.firstChildElement();
	while (!sousElem.isNull())
	{
		QDomElement	sousElem2;
		if (sousElem.tagName() == "move" && (sousElem.attribute("type") == "TM/HM" || \
												sousElem.attribute("type") == "level-up"))
		{
			lvl = 1;
			name = "";
			sousElem2 = sousElem.firstChildElement();
			while (!sousElem2.isNull())
			{
				if (sousElem2.tagName() == "name")
					name = sousElem2.text().toStdString().c_str();
				else if (sousElem2.tagName() == "lvl")
					lvl = atoi(sousElem2.text().toStdString().c_str());
				sousElem2 = sousElem2.nextSiblingElement();
			}
			tab.push_back(make_pair(name, lvl));		
		}
		sousElem = sousElem.nextSiblingElement();
	}
	currPokemon->setSkills(tab);
	return ;
}
